package com.ezops.dtcc.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;

import com.ezops.dtcc.model.DtccPosition;
import com.ezops.dtcc.model.DtccRules;
import com.ezops.dtcc.model.DtccRulesSql;
import com.ezops.dtcc.rowmapper.DtccRulesDefRowMapper;
import com.ezops.dtcc.rowmapper.DtccRulesRowMapper;

public class DtccRulesDAO implements ApplicationContextAware {
	
	@SuppressWarnings("unused")
	private ApplicationContext context;
	
	private final static Logger LOGGER = Logger.getLogger(DtccRulesDAO.class);
	private JdbcTemplate jdbcTemplate;

	public DtccRulesDAO(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void insert(DtccRules rule) {
	
	}
	
	
	public List<DtccRules> findAll() {
		
		
		String selectHuginUser = "SELECT * DTCC_RULES";
		List<Object> resultList = this.jdbcTemplate.query(selectHuginUser,  new DtccRulesDefRowMapper());
		List<DtccRules> ruleList = new ArrayList<DtccRules>();
		if (resultList != null && !resultList.isEmpty()) {
		    for (int i = 0; i < resultList.size(); i++) {
		    	ruleList.add((DtccRules) resultList.get(i));		
		    }
		}	
		return ruleList;
		
		
	}
	
	public DtccRules findById(int ruleId) {

		DtccRules foundItem = null;
		String selectRule = "SELECT * DTCC_RULES = " + ruleId;
		List<Object> resultList = this.jdbcTemplate.query(selectRule,  new DtccRulesRowMapper());
		if(!resultList.isEmpty() ) 
			foundItem = (DtccRules)resultList.get(0);
		
		return foundItem;
		
	}
	
	
	public void executeAllRules()
    {
		 //Get the list of report rules to execute
		 List<DtccRules> ruleToExecuteList = this.findAll();
		 
		 //Loop on the rule list and execute each one
		 for( DtccRules dtccRule : ruleToExecuteList ) {
			 this.execute(dtccRule);
		 }
		
    }
	
	
	
	
	public void execute(DtccRules rule )
    {
           
           //Get the sql stmt for this rule
		   DtccRulesSqlDAO dtccRulesSqlDAO = (DtccRulesSqlDAO)context.getBean("dtccRuleSqlDAO");
		   DtccRulesSql dtccRuleSql = dtccRulesSqlDAO.findById(rule.getRuleId());

		   //DtccPostionsDAO dtccPostionsDAO = (DtccPostionsDAO)context.getBean("dtccPostionsDAO");
		   
           //Select the postions using the select stmt where claus for the rule
		   //List<DtccPostions>  postionsList = dtccPostionsDAO.findBySql(dtccRuleSql);
		   
           //Insert the exceptions found for the rule from the postions returned for this rule
		   //Pass in the ruleSql with the ruleId
		   //  	dtccExceptions.addPostions(postionsList,dtccRuleSql) 
	
		   DtccExceptionDAO dtccExceptionsDAO = (DtccExceptionDAO)context.getBean("dtccExceptionsDAO");

           //Select the postions using the select stmt where claus for the rule
		   //List<DtccPostions>  postionsList = dtccPostionsDAO.findBySql(dtccRuleSql);
		   //DUMMY for Compiling
		   List<DtccPosition>  postionsList = new ArrayList<DtccPosition>();
		   dtccExceptionsDAO.addPositionExceptions(rule.getRuleId(), postionsList);
		   
		   

    }

	@Override
	public void setApplicationContext(ApplicationContext beanCtx) 	throws BeansException {
		this.context = beanCtx;
		
	}
	
	
	

}
